package com.amtf.datafile3.cfg.xsv;

import java.text.DateFormat;
import java.text.Format;
import java.text.NumberFormat;

import com.amtf.datafile3.DataField;
import com.amtf.datafile3.DataFieldType;
import com.amtf.datafile3.cfg.std.StdDataFieldDefinition;
import com.amtf.datafile3.core.std.type.DateFieldType;
import com.amtf.datafile3.core.std.type.DecimalFieldType;
import com.amtf.datafile3.core.std.type.StringFieldType;
import com.amtf.datafile3.core.xsv.parser.AbsXsvDataFieldParser;
import com.amtf.datafile3.core.xsv.parser.XsvDateFieldParser;
import com.amtf.datafile3.core.xsv.parser.XsvDecimalFieldParser;
import com.amtf.datafile3.core.xsv.parser.XsvStringFieldParser;
import com.amtf.datafile3.core.xsv.render.XsvDataFieldRender;

public class XsvDataFieldDefinition extends StdDataFieldDefinition {

	public XsvDataFieldDefinition(String name, DataFieldType type, char delimiter, char quote, boolean forceQuote, boolean trimValue, Format informat, Format outformat) {
		super(name, type, createParser(type, quote, trimValue, informat), new XsvDataFieldRender(outformat, quote, delimiter, forceQuote));
	}

	@Override
	public XsvDataFieldDefinition clone() {
		XsvDataFieldDefinition newObj = new XsvDataFieldDefinition(name, null, ' ', ' ', false, false, null, null);
		copyTo(newObj);
		return newObj;
	}

	private static AbsXsvDataFieldParser<?, ?> createParser(DataFieldType type, char quote, boolean trimValue, Format informat) {
		AbsXsvDataFieldParser<? extends DataField, ?> fieldParser = null;
		if (type instanceof StringFieldType) {
			fieldParser = new XsvStringFieldParser(quote, trimValue);
		} else if (type instanceof DecimalFieldType) {
			fieldParser = new XsvDecimalFieldParser((NumberFormat) informat, quote, trimValue);
		} else if (type instanceof DateFieldType) {
			fieldParser = new XsvDateFieldParser((DateFormat) informat, quote, trimValue);
			// } else if (type instanceof ObjectFieldType) {
			// fieldParser = new XsvStringFieldParser(quote, trimValue);
		} else {
			fieldParser = new XsvStringFieldParser(quote, trimValue);
		}

		return fieldParser;
	}

	public char getDelimiter() {
		return ((XsvDataFieldRender) render).getDelimiter();
	}

	public void setDelimiter(char delimiter) {
		((XsvDataFieldRender) render).setDelimiter(delimiter);
	}

	public char getQuote() {
		return ((XsvDataFieldRender) render).getQuote();
	}

	public void setQuote(char quote) {
		((XsvDataFieldRender) render).setQuote(quote);
		((AbsXsvDataFieldParser) parser).setQuote(quote);
	}

	public boolean isForceQuote() {
		return ((XsvDataFieldRender) render).isForceQuote();
	}

	public void setForceQuote(boolean forceQuote) {
		((XsvDataFieldRender) render).setForceQuote(forceQuote);
	}

	public boolean isTrimValue() {
		return ((AbsXsvDataFieldParser) parser).isTrimValue();
	}

	public void setTrimValue(boolean trimValue) {
		((AbsXsvDataFieldParser) parser).setTrimValue(trimValue);
	}

	public Format getOutformat() {
		return ((XsvDataFieldRender) render).getFormat();
	}

	public void setOutformat(Format outformat) {
		((XsvDataFieldRender) render).setFormat(outformat);
	}

	public Format getInformat() {
		return ((AbsXsvDataFieldParser) parser).getFormat();
	}

	public void setInformat(Format informat) {
		((AbsXsvDataFieldParser) parser).setFormat(informat);
	}

}
